/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       environment.cpp
 * Author:     karooolek
 * Created on: Jun 23, 2009
 *
 **********************************************************************************************************************/

#include "environment.h"
#include "../input.h"
//#include <gl/glut.h>

namespace mGameEngine
{

namespace Graphics
{


Environment::Environment() :
    Drawable(), _environmentMap(NULL)
{
    _boundingSphere.setRadius(9999999999.9f);
}

Environment::Environment(const Texture *environmentMap) :
    Drawable(), _environmentMap(environmentMap)
{
    _boundingSphere.setRadius(9999999999.9f);
}

Environment::~Environment()
{
}

void Environment::draw() const
{
    // create display list
    static uint dl = 0;
    if(!dl)
    {
        dl = glGenLists(1);
        glNewList(dl, GL_COMPILE);

//        glDisable(GL_DEPTH_TEST);
//        glutSolidSphere(1.0f, 16, 16);
//        glEnable(GL_DEPTH_TEST);

        // vertices
        static const Vector3 vertices[] =
        {
            Vector3(-0.5f, -0.5f, -0.5f),
            Vector3(0.5f, -0.5f, -0.5f),
            Vector3(0.5f, 0.5f, -0.5f),
            Vector3(-0.5f, 0.5f, -0.5f),
            Vector3(-0.5f, -0.5f, 0.5f),
            Vector3(0.5f, -0.5f, 0.5f),
            Vector3(0.5f, 0.5f, 0.5f),
            Vector3(-0.5f, 0.5f, 0.5f),
        };

        // indices
        static const uint8 indices[] =
        {
            6, 2, 1, 5,
            0, 3, 7, 4,
            7, 3, 2, 6,
            5, 1, 0, 4,
            6, 5, 4, 7,
            0, 1, 2, 3
        };

        glDisable(GL_DEPTH_TEST);
        glBegin(GL_QUADS);
            for(uint8 i = 0; i != 24; ++i)
            {
                glVertex3fv(vertices[indices[i]]);
            }
        glEnd();
        glEnable(GL_DEPTH_TEST);

        glEndList();
    }

    // create gpu program
    static GPUProgram gpuProgram(
#include "environment.vert"
        ,
#include "environment.frag"
        , true);

    // uss gpu program
    gpuProgram.use();
    gpuProgram.setTexture("environmentMap", _environmentMap, 4);
    
    // draw cube
    glCallList(dl);
}

}

}

